package com.zzty.servlet;

import com.zzty.dao.QuestionDao;
import com.zzty.dao.TeamExamDao;
import com.zzty.daoimpl.QuestionDaoImpl;
import com.zzty.daoimpl.TeamExamDaoImpl;
import com.zzty.entity.HistoryGroup;
import com.zzty.entity.User;
import com.zzty.service.GroupService;

import com.zzty.service.TeamExamTransactionService;
import com.zzty.serviceimpl.GroupServiceImpl;

import com.zzty.serviceimpl.TeamExamTransactionServiceImpl;
import org.codehaus.jackson.map.ObjectMapper;
import sun.swing.BakedArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "/HistoryMyGroupServlet", urlPatterns = "/HistoryMyGroupServlet")
public class HistoryMyGroupServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        if ("getTable".equals(method)) {
            getTable(request, response);
        }
    }

    private void getTable(HttpServletRequest request, HttpServletResponse response) {

        //String name = (String) request.getSession().getAttribute("name");   //
        User user=(User)request.getSession().getAttribute("user");
        String name=user.getName();
        GroupService gs = new GroupServiceImpl();
        TeamExamTransactionService tets = new TeamExamTransactionServiceImpl();
        Integer groupId = gs.getIdByUserName(name);
        if (groupId != null) {
            List<HistoryGroup> historyGroups = tets.getHistoryGroupByGroupId(groupId);
            ObjectMapper mapper = new ObjectMapper();
            String json = null;
            try {
                json = mapper.writeValueAsString(historyGroups);
                PrintWriter out = response.getWriter();
                out.write(json);
                out.flush();
                out.close();
                System.out.println("111 " + historyGroups.size());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
